function splitPath( path)
    local pos2 = string.find( path, "/", 1)
    if pos2 == nil then
        pos2 = string.find( path, "\\", 1)
    end
    local pos = pos2
    while pos2 ~= nil do
        local t = pos2 + 1
        pos2  = string.find( path, "/", t)
        if pos2 == nil then
            pos2 = string.find( path, "\\", t)
        end
        if pos2 then
            pos = pos2
        end
    end
    if pos then
        return string.sub( path, 1, pos), string.sub( path, pos + 1)
    else
        return "", path
    end
end

function getParentDirName( path)
    local p = splitPath( path)
    if p == nil then
        return "" 
    end
    local _, p2 = splitPath( string.sub(p, 1, #p -1))
    return p2
end

function find_last( str, pattern)
    local lasti = nil
    local lastj = 0 
    local i, j

    while true do
        i, j = string.find( str, pattern, lastj + 1, true)
        if i == nil then
            return lasti, lastj
        else
            lasti, lastj = i, j
        end
    end
end

function find_num( str, pattern)
    local lasti = nil
    local lastj = 0 
    local i, j
    local count = 0
    while true do
        i, j = string.find( str, pattern, lastj + 1, true)
        if i == nil then
            return count
        else
            count = count + 1
            lasti, lastj = i, j
        end
    end
end

function noSuffixFileName( path)
    local i, j = find_last( path, "/")
    if i == nil then
        j = 1
    end
    local k, h = find_last( path , ".")
    if k == nil then
        h = #path 
    end
    if  h < k then
        return string.sub( path, j + 1)
    else
        return string.sub( path, j + 1, h - 1)
    end
end

function numbertobytes( num, width)
    if num < 0 then
        num = math.pow(2, width * 8) + num
    end
    local function _n2b( width, num, rem)
        rem = rem * 256
        if width == 0 then return rem end
        return rem, _n2b(width-1, math.modf(num/256))
    end
    return string.char(_n2b(width-1, math.modf(num/256)))
end

function numbertofixstr( num, width)
    local s = tostring( num)
    local n
    for n = #s, width - 1 do
        s = "0" .. s
    end
    return s
end
